const express = require('express')
const path = require('path')
const logger = require('morgan');
const mongoose = require('mongoose')
const port = process.env.PORT || 4000
const app = express()
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const session = require('express-session')
const mongoStore = require('connect-mongo')(session)
app.locals.moment = require('moment')
const dbUrl = 'mongodb://localhost/movie'

mongoose.connect(dbUrl, {useNewUrlParser:true}, function(err){
　　if(err){
　　　　console.log('db Connect fail Error:' + err)
　　}else{
　　　　console.log('db Connect success') }
})
app.set('views', './app/views/pages')
app.set('view engine', 'jade')
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(cookieParser())
app.use(session({
  secret: 'movie',
  store: new mongoStore({
    url : dbUrl,
    collection: 'sessions'
  })
}))

app.use(express.static(path.join(__dirname, 'public')))

if ('development' === app.get('env')) {
  app.set('showStackError', true)
  app.use(logger(':method :url :status'))
  app.locals.pretty = true
  mongoose.set('debug', true)
}


require('./config/routes')(app)

app.listen(port, err => {
  err ? console.log(err) : console.log(`服务在${port}端口启动成功`)
})